﻿Imports System.IO
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services

Public Class filemanServices
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <System.Web.Services.WebMethod(EnableSession:=True)> Public Shared Function getFiles() As Object()
        Return getFiles(filemanServices.getRootPath)
    End Function
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <System.Web.Services.WebMethod(EnableSession:=True)> Public Shared Function deleteFile(filename As String, path As String) As Boolean
        Dim fullFilename As String = path & "\" & filename
        If IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(fullFilename)) Then
            IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(fullFilename))
            If thumbFolderExists(path) Then
                If thumbExists(path, filename) Then
                    deleteThumb(path, filename)
                End If
            End If
        End If
        Return True
    End Function
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <System.Web.Services.WebMethod(EnableSession:=True)> Public Shared Function renameFile(filename As String, path As String, newName As String) As Boolean
        Dim fullFilename As String = path & "\" & filename
        Dim newFullFileName As String = path & "\" & newName
        If IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(fullFilename)) Then
            IO.File.Copy(System.Web.HttpContext.Current.Server.MapPath(fullFilename), System.Web.HttpContext.Current.Server.MapPath(newFullFileName))
            IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(fullFilename))
        End If
        Return True
    End Function
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <System.Web.Services.WebMethod(EnableSession:=True)> Public Shared Function copyFile(filename As String, path As String) As Boolean
        Dim fullFilename As String = path & "\" & filename
        Dim newFullFileName As String = path & "\" & filename.Substring(0, filename.LastIndexOf(".")) & "_copy" & filename.Substring(filename.LastIndexOf("."))
        If IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(fullFilename)) Then
            IO.File.Copy(System.Web.HttpContext.Current.Server.MapPath(fullFilename), System.Web.HttpContext.Current.Server.MapPath(newFullFileName))
        End If
        Return True
    End Function
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <System.Web.Services.WebMethod(EnableSession:=True)> Public Shared Function moveFile(filename As String, path As String, newPath As String) As Boolean
        Dim fullFilename As String = path & "\" & filename
        Dim newFullFileName As String = ""
        If newPath <> "" Then
            newFullFileName = filemanServices.getRootPath & "\" & newPath & "\" & filename
        Else
            newFullFileName = filemanServices.getRootPath & "\" & filename
        End If
        If IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(fullFilename)) Then
            IO.File.Copy(System.Web.HttpContext.Current.Server.MapPath(fullFilename), System.Web.HttpContext.Current.Server.MapPath(newFullFileName))
            IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(fullFilename))
        End If
        Return True
    End Function
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <System.Web.Services.WebMethod(EnableSession:=True)> Public Shared Function addFolder(folder As String, path As String) As Boolean
        Dim fullFilename As String = "Files\" & path.Substring(path.IndexOf("\") + 1) & "\" & folder
        If Not IO.Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(fullFilename)) Then
            IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(fullFilename))
        End If
        Return True
    End Function
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <System.Web.Services.WebMethod(EnableSession:=True)> Public Shared Function upload(file As HttpPostedFileBase) As String
        Dim filename As String = "~/" & filemanServices.getRootPath & "/" & file.FileName
        file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(filename))
        Return "OK"
    End Function

    Public Shared Function getRootPath() As String
        Dim path As String = System.Web.HttpContext.Current.Session("rootPath")
        If (path = "") Or (path Is Nothing) Then
            path = filemanServices.readConfigSetting("rootPath")
            System.Web.HttpContext.Current.Session("rootPath") = path
        End If
        Return path
    End Function
    Public Shared Function getIconPath() As String
        Dim iconPath As String = System.Web.HttpContext.Current.Session("iconPath")
        If (iconPath = "") Or (iconPath Is Nothing) Then           
            iconPath = filemanServices.readConfigSetting("iconPath")
            System.Web.HttpContext.Current.Session("iconPath") = iconPath

        End If
        Return iconPath
    End Function

    Public Shared Function readConfigSetting(setting As String) As String
        Dim value As String = ""
        Dim rootConf As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config")
        If 0 < rootConf.AppSettings.Settings.Count Then
            Dim oSetting As System.Configuration.KeyValueConfigurationElement = rootConf.AppSettings.Settings(setting)
            If Not (oSetting.Value = Nothing) Then
                value = oSetting.Value
            End If
        End If
        Return value
    End Function

    <System.Web.Http.HttpPost> Public Shared Sub upload()
        MsgBox("test")
    End Sub

    Private Shared Function getFiles(path As String, Optional level As Integer = 1) As clsFile()
        Dim rFiles() As clsFile = {}
        Dim imageTypes As List(Of String) = {".jpg", ".jpeg", ".bmp", ".png", ".gif"}.ToList
        Dim dirinfo As New DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(path))
        Dim directories() As DirectoryInfo = dirinfo.GetDirectories()
        Dim files() As FileInfo = dirinfo.GetFiles()
        For Each directory As DirectoryInfo In directories
            If directory.Name.Substring(0, 1) <> "_" Then
                Dim nfile As New clsFile
                Dim spath As String = ""
                If level > 1 Then
                    spath = "root\" & path.Substring(path.IndexOf("\")) & "\" & directory.Name
                Else
                    spath = "root\"
                End If
                nfile.name = directory.Name
                nfile.type = "folder"
                nfile.level = level
                nfile.path = spath
                nfile.thumbnail = "\" & filemanServices.getIconPath & "\folder.png"
                nfile.content = getFiles(path & "\" & directory.Name, level + 1)
                Array.Resize(rFiles, rFiles.Length + 1)
                rFiles(rFiles.Length - 1) = nfile
            End If
        Next
        For Each file As FileInfo In files
            Dim nFile As New clsFile
            nFile.name = file.Name
            nFile.type = file.Extension
            nFile.level = level
            nFile.path = path
            If IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath("~\" & filemanServices.getIconPath & "\" & file.Extension.Substring(1) & ".png")) Then
                nFile.thumbnail = "\" & filemanServices.getIconPath & "\" & file.Extension.Substring(1) & ".png"
            Else
                nFile.thumbnail = "\" & filemanServices.getIconPath & "\default.png"
            End If
            If imageTypes.Contains(file.Extension.ToLower) Then
                If Not thumbFolderExists(path) Then
                    createThumbnailFolder(path)
                End If
                If Not thumbExists(path, file.Name) Then
                    buildThumbnail(path & "\" & file.Name, path, file.Name)
                End If
                nFile.thumbnail = path & "\_thumbs\" & file.Name
            End If
            Array.Resize(rFiles, rFiles.Length + 1)
            rFiles(rFiles.Length - 1) = nFile
        Next
        Return rFiles
    End Function

    Private Shared Function thumbExists(path As String, file As String) As Boolean
        Dim ok As Boolean = False
        Dim thumbPath As String = path & "\_thumbs\" & file
        If IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(thumbPath)) Then
            ok = True
        End If
        Return ok
    End Function
    Private Shared Function deleteThumb(path As String, file As String) As Boolean
        Dim ok As Boolean = True
        Dim thumbPath As String = path & "\_thumbs\" & file
        IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(thumbPath))
        Return ok
    End Function
    Private Shared Function thumbFolderExists(path As String) As Boolean
        Dim ok As Boolean = False
        Dim thumbPath As String = path & "\_thumbs"
        If IO.Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(thumbPath)) Then
            ok = True
        End If
        Return ok
    End Function
    Private Shared Sub createThumbnailFolder(path As String)
        System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(path) & "\_thumbs")
    End Sub
    Private Shared Sub buildThumbnail(imagePath As String, outputPath As String, imageName As String)
        Dim sourceImage As New System.Drawing.Bitmap(System.Web.HttpContext.Current.Server.MapPath(imagePath))
        Dim sourceW As Integer = sourceImage.Width
        Dim sourceH As Integer = sourceImage.Height
        Dim targetW As Integer = 100
        Dim targetH As Integer = 100
        If sourceW > sourceH Then
            targetH = (targetW / sourceW) * sourceH
        ElseIf sourceH > sourceW Then
            targetW = (targetH / sourceH) * sourceW
        End If
        Dim myCallback As New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
        Dim thumbnail As System.Drawing.Image = sourceImage.GetThumbnailImage(targetW, targetH, myCallback, IntPtr.Zero)
        thumbnail.Save(System.Web.HttpContext.Current.Server.MapPath(outputPath) & "\_thumbs\" & imageName)
        sourceImage.Dispose()
        thumbnail.Dispose()
    End Sub




    Public Shared Function ThumbnailCallback() As Boolean
        Return False
    End Function
End Class